package com.ytb.metadonnee;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import com.liferay.portal.kernel.googleapps.comparator.GGroupEmailAddressComparator;

public class XmlMetadonneeParser {
	static final String META = "meta";
	static final String AUTHOR = "author";
	static final String TITLE = "title";
	static final String HEIGHTVIDEO = "heightVideo";
	static final String WIDTHVIDEO = "widthVideo";
	static final String SIZE = "size";
	static final String LENGTHVIDEO = "lengthVideo";
	static final String CREATION_DATE = "creationDate";
	static final String MODIFICATION_DATE = "modificationDate";
	static final String GENERAL = "general";
	static final String KEYWORD = "keyword";
	
	
	private static String title = "";
	private static String author ="";
	private static String creationDate =""; 
	private static String modificationDate =""; 
	private static int videoHeight = 0; 
	private static int videoLength = 0;
	private static int videoSize = 0;
	private static int videoWidth = 0;
	
	
	/**
	 * Open the <b>metadonne.xml</b> document and read its content.
	 * @param metaFile
	 * 	The path of XML document.
	 * @throws XMLStreamException
	 * @throws IOException
	 */
	public static XmlMetadonnee parse(String metaFile, XmlMetadonnee xmlMeta) {
		XmlMetadonnee xmlM = null;
		XMLInputFactory factory = XMLInputFactory.newInstance();
		InputStream is = null;
		XMLStreamReader reader = null;
		try {
			is = (new URL(metaFile)).openConnection().getInputStream();
			reader = factory.createXMLStreamReader(is);
			xmlM = readXmlMetadonnee(reader, xmlMeta);
		} catch (IOException | XMLStreamException e) {
			e.printStackTrace();
		} finally { // Close the flow
			try {
				reader.close();
				is.close();
			} catch (IOException | XMLStreamException e) {
				e.printStackTrace();
			}
		}
		return xmlM;
	}

	private static XmlMetadonnee readXmlMetadonnee(XMLStreamReader reader, XmlMetadonnee xmlMeta)throws XMLStreamException, IOException {
		
		XmlMetadonneeGeneral xmlMetaGeneral = new XmlMetadonneeGeneral();
		
		while(reader.hasNext())
		{
			int event = reader.next();
			switch(event)
			{
				case XMLStreamConstants.START_ELEMENT:
					switch(reader.getLocalName()) 
					{
					case META : 
						break;
					case GENERAL : 
						break;
					case AUTHOR : 
						author = reader.getElementText();
						break;
					case TITLE : 
						title = reader.getElementText();
						break;
					case HEIGHTVIDEO : 
						videoHeight = Integer.parseInt(reader.getElementText());
						break;
					case WIDTHVIDEO : 
						videoWidth = Integer.parseInt(reader.getElementText());
						break;
					case SIZE : 
						videoSize = Integer.parseInt(reader.getElementText());
						break;
					case LENGTHVIDEO : 
						videoLength = Integer.parseInt(reader.getElementText());
						break;
					case CREATION_DATE : 
						creationDate = reader.getElementText();
						break;
					case MODIFICATION_DATE : 
						modificationDate = reader.getElementText();
						break;
					case KEYWORD : 
						xmlMeta.getMetadonneeKeywords().add(new XmlMetadonneeKeyword(reader.getElementText()));
						break;
					default: break;
					}
				case XMLStreamConstants.END_ELEMENT:
					switch(reader.getLocalName()){
					case META : 
						break;
					case GENERAL : 
						break;
					case AUTHOR : 
						xmlMetaGeneral.setAuthor(author);
						break;
						case TITLE : 
							xmlMetaGeneral.setTitle(title);
							break;
						case HEIGHTVIDEO : 
							xmlMetaGeneral.setVideoHeight(videoHeight);
							break;
						case WIDTHVIDEO : 
							xmlMetaGeneral.setVideoWidth(videoWidth);
							break;
						case SIZE : 
							xmlMetaGeneral.setVideoSize(videoSize);
							break;
						case LENGTHVIDEO :
							xmlMetaGeneral.setVideoLength(videoLength);
							break;
						case CREATION_DATE : 
							xmlMetaGeneral.setCreationDate(creationDate);
							break;
						case MODIFICATION_DATE : 
							xmlMetaGeneral.setModificationDate(modificationDate);
							break;
						case KEYWORD : 
							break;
						default: 
							break;
							
					}
			}
		}
		
		xmlMeta.setMetadonneeGeneral(xmlMetaGeneral);
		return xmlMeta;
	}
}
